import 'package:flutter/material.dart';

import '../dto/assessment.response.dart';
import 'PlacePlayerWidget.dart';

class PlaceSelectGroup extends StatelessWidget {
  final List<QuestionContent> items;
  final String value;
  final ValueChanged<String> onChanged;

  const PlaceSelectGroup(
      {Key? key,
      required this.items,
      required this.value,
      required this.onChanged})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return IntrinsicHeight(
        child: Row(children: [
      for (int i = 0; i < items.length; i++)
        _buildSelectItem(items[i], value == items[i].baseValue, context)
    ]));
  }

  Widget _buildSelectItem(
      QuestionContent body, bool isSelect, BuildContext context) {
    var widget = Column(
      children: [
        SizedBox(height: 15),
        Image.network(body.image!, height: 100, width: 100),
        Padding(
            padding: EdgeInsets.only(left: 3, right: 3),
            child: _buildTextAudio(body.text!, body.audio)),
        SizedBox(height: 10)
      ],
    );
    final screenWidth = MediaQuery.of(context).size.width;
    return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () => onChanged(body.baseValue ?? ''),
        child: Container(
            margin: EdgeInsets.only(left: 10, right: 10),
            width: (screenWidth - 225) * 0.25,
            child: widget,
            decoration: BoxDecoration(
                color: Colors.white,
                border: Border.all(color: Color(0xff666666), width: 1),
                boxShadow: [
                  isSelect
                      ? BoxShadow(
                          color: Color(0xff73af75),
                          offset: Offset(0, 1),
                          blurRadius: 1,
                          spreadRadius: 3)
                      : BoxShadow(color: Colors.transparent)
                ],
                borderRadius: BorderRadius.circular(8))));
  }

  Widget _buildTextAudio(String text, String? audio) {
    return audio == null
        ? Text(text,
            textAlign: TextAlign.center,
            style: TextStyle(
                fontSize: 15,
                height: 1.5,
                color: Color(0xff666666),
                fontWeight: FontWeight.w600))
        : RichText(
            textAlign: TextAlign.center,
            text: TextSpan(
                text: text,
                style: TextStyle(
                    fontSize: 15,
                    height: 1.5,
                    color: Color(0xff666666),
                    fontWeight: FontWeight.w600),
                children: [
                  WidgetSpan(
                      alignment: PlaceholderAlignment.middle,
                      child: PlacePlayerWidget(
                          audioUrl: audio, isShowLoading: false))
                ]));
  }
}
